//
//  UIImage+WLImagel.m
//  HengYouCai
//
//  Created by wll on 2017/11/8.
//  Copyright © 2017年 lwj. All rights reserved.
//

#import "UIImage+WLImagel.h"

@implementation UIImage (WLImagel)

+ (instancetype)wl_getLaunchImage{
    CGSize viewSize = [UIScreen mainScreen].bounds.size;
    NSString * viewOrientation = @"Portrait";
    NSString * launchImage = nil;
    NSArray * imageDictArr = [[NSBundle mainBundle].infoDictionary valueForKey:@"UILaunchImages"];
    int i = 0;
    for (NSDictionary * dic in imageDictArr) {
        i++;
        CGSize imageSize = CGSizeFromString(dic[@"UILaunchImageSize"]);
        if (CGSizeEqualToSize(imageSize, viewSize)
            && [viewOrientation isEqualToString:dic[@"UILaunchImageOrientation"]]) {
            launchImage = dic[@"UILaunchImageName"];
            break;
        }
    }
    return [UIImage imageNamed:launchImage];
}

/** 根据字符串生成指定颜色大小的黑白二维码 */
+ (UIImage *)wl_createQRImageWithString:(NSString *)string bgColor:(UIColor *)bgColor contentColor:(UIColor *)ctColor size:(CGSize)size{
    NSData *stringData = [string dataUsingEncoding:NSUTF8StringEncoding];
    CIFilter *qrFilter = [CIFilter filterWithName:@"CIQRCodeGenerator"];
    [qrFilter setValue:stringData forKey:@"inputMessage"];
    [qrFilter setValue:@"M" forKey:@"inputCorrectionLevel"];
    
    CIFilter *colorFilter = [CIFilter filterWithName:@"CIFalseColor" keysAndValues:@"inputImage", qrFilter.outputImage,@"inputColor0",ctColor?[CIColor colorWithCGColor:ctColor.CGColor]:[CIColor colorWithCGColor:[UIColor blackColor].CGColor],@"inputColor1",bgColor?[CIColor colorWithCGColor:bgColor.CGColor]:[CIColor colorWithCGColor:[UIColor whiteColor].CGColor],nil];
    CIImage *qrImage = colorFilter.outputImage;
    //放大并绘制二维码 (上面生成的二维码很小，需要放大)
    CGImageRef cgImage = [[CIContext contextWithOptions:nil] createCGImage:qrImage fromRect:qrImage.extent];
    UIGraphicsBeginImageContext(size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetInterpolationQuality(context, kCGInterpolationNone);
    //翻转一下图片 不然生成的QRCode就是上下颠倒的
    CGContextScaleCTM(context, 1.0, -1.0);
    CGContextDrawImage(context, CGContextGetClipBoundingBox(context), cgImage);
    UIImage *codeImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    CGImageRelease(cgImage);
    
    return codeImage;
}






@end
